package com.youhome.api.controller;

import com.github.pagehelper.PageInfo;
import com.youhome.api.dto.TimeValueItemResult;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.dto.DTOCompanyReport;
import com.youhome.service.model.CompanyReport;
import com.youhome.service.model.CustomCompanyTokenDetails;
import com.youhome.service.service.CompanyReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 房源报告
 */
@RestController
@Api(tags = "报告")
@RequestMapping("/homes")
public class ReportController {
    @Autowired
    private CompanyReportService companyReportService;

    @ApiOperation(value = "报告详情", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "reportId", value = "报告ID", required = true, paramType = "path")
    })
    @GetMapping("/report/{reportId}")
    public Object detail(@PathVariable Long reportId,
                         @ApiIgnore @AuthenticationPrincipal CustomCompanyTokenDetails companyTokenDetails) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, companyReportService.selectByPrimaryKey(reportId, companyTokenDetails));
    }

    @ApiOperation(value = "生成报告", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "reportType", value = "报告类型，0免费，1收费", required = true, paramType = "query")
    })
    @PostMapping(value = "/report")
    public Object generate(String houseId, Integer reportType, @ApiIgnore @AuthenticationPrincipal CustomCompanyTokenDetails companyTokenDetails) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, companyReportService.generate(companyTokenDetails, reportType, houseId));
    }

    @ApiOperation(value = "报告列表", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageIndex", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页大小", paramType = "query")
    })
    @GetMapping(value = "/report")
    public Object list(Integer pageIndex, Integer pageSize,
                       @ApiIgnore @AuthenticationPrincipal CustomCompanyTokenDetails companyTokenDetails) {
        CompanyReport record = new CompanyReport();
        record.setCompanyId(companyTokenDetails.getCompanyId());
        PageInfo<DTOCompanyReport> pageInfo = companyReportService.selectByBean(pageIndex, pageSize, record);
        return new ResultPage<>(ResultCode.SUCCESS, pageInfo);
    }
}
